package com.house.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.house.pojo.County;
import com.house.pojo.House;
import com.house.pojo.Street;
import com.house.pojo.Type;
import com.house.pojo.User;
import com.house.pojo.PageCtl;
import com.house.service.CountyService;
import com.house.service.HouseService;
import com.house.service.StreetService;
import com.house.service.TypeService;
import com.house.service.UserService;



@Controller
public class HouseController {
	
	
	@Autowired
	private HouseService houseService;
	@Autowired
	private TypeService typeService;
	@Autowired
	private CountyService countyService;
	@Autowired
	private StreetService streetService;
	@Autowired
	private UserService userServcie;
	
	
	
	
	
	
	
	private static final long serialVersionUID = 1L;
    private Map<String, Object> param = new HashMap<String,Object>();
    private static PageCtl page = new PageCtl();//页面信息类
    
    
    
    
    @RequestMapping("/next")
	private ModelAndView nextPage(Integer index,@RequestParam(name="title")String hTitle,@RequestParam(name="district_id")Integer h_countyno,@RequestParam(name="street_id")Integer h_streetno,@RequestParam(name="type_id")Integer h_typeno,@RequestParam(name="price")String hPrice,@RequestParam(name="floorage")String hArea) {
    	index++;
		page.setPageNum(index);
		return findHouse(hTitle,h_countyno,h_streetno,h_typeno,hPrice,hArea);
	}
	@RequestMapping("/prev")
	private ModelAndView prevPage(Integer index,@RequestParam(name="title")String hTitle,@RequestParam(name="district_id")Integer h_countyno,@RequestParam(name="street_id")Integer h_streetno,@RequestParam(name="type_id")Integer h_typeno,@RequestParam(name="price")String hPrice,@RequestParam(name="floorage")String hArea)  {
		index--;
		page.setPageNum(index);
		System.out.println("--------------------------"+hTitle+h_countyno+h_streetno+h_typeno+hPrice+hArea);
		return findHouse(hTitle,h_countyno,h_streetno,h_typeno,hPrice,hArea);
	}
	@RequestMapping("/first")
	private ModelAndView firstPage(@RequestParam(name="title")String hTitle,@RequestParam(name="district_id")Integer h_countyno,@RequestParam(name="street_id")Integer h_streetno,@RequestParam(name="type_id")Integer h_typeno,@RequestParam(name="price")String hPrice,@RequestParam(name="floorage")String hArea)  {
		page.setPageNum(1);
		return findHouse(hTitle,h_countyno,h_streetno,h_typeno,hPrice,hArea);
	}
	@RequestMapping("/last")
	private ModelAndView lastPage(@RequestParam(name="title")String hTitle,@RequestParam(name="district_id")Integer h_countyno,@RequestParam(name="street_id")Integer h_streetno,@RequestParam(name="type_id")Integer h_typeno,@RequestParam(name="price")String hPrice,@RequestParam(name="floorage")String hArea)  {
		page.setPageNum(page.getPages());
		return findHouse(hTitle,h_countyno,h_streetno,h_typeno,hPrice,hArea);
	}
	
	
	
	
	
	//在新增之前将面积类型和区信息查出来进行显示
	@RequestMapping("/fabu")
	public ModelAndView typeAndCounty(Integer userno) {
		
		ModelAndView mov = new ModelAndView();
		
		 List<Type> tist = typeService.selectType();//查找出所有的房屋类型
		 List<County> clist = countyService.selectCounty();//查找出所有的区信息
		 User user = userServcie.selectUser(userno);//查找出当前用户的信息
		 
		 if(tist!=null&&clist!=null) {
			 mov.addObject("tlist", tist);
			 mov.addObject("clist",clist);
			 mov.addObject("user", user);
			 mov.setViewName("fabu");
		 }
		
		return mov;
	}
	
	
	
	
	
	
	
	
	
	//用AJAX查找街道信息
	@ResponseBody
	@RequestMapping("/findStreet")
	public List<Street> findStreet(Integer countyno){
		
		List<Street> slist = streetService.selectStreet(countyno);//查找出所有的街道信息
		
		return slist;//因为有jackson所以会自动转换成json类型
	}
	
	
	
	
	
	
	
	
	
	
	//新增或者修改房屋信息
	@RequestMapping("/addHouseOrUpdate")
	public ModelAndView addHouse(Integer houseno,Integer userno,String title,Integer type_id,double floorage,double price,String houseDate,Integer district_id,Integer street_id,Integer contact,String description) {
		
		ModelAndView mov = new ModelAndView(); 
		
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("hTitle", title);//标题
		map.put("h_typeno", type_id);//户型
		map.put("hArea", floorage);//面积
		map.put("hPrice", price);//价格
		map.put("hDate", houseDate);//房产证日期
		map.put("h_countyno", district_id);//区号
		map.put("h_streetno", street_id);//街道编号
		map.put("hTel", contact);//联系方式
		map.put("hDescription", description);//详细信息
		map.put("h_userno", userno);//用户编号
		map.put("hPictrue", "thumb_house.gif");//图片
		
		
		if(houseno!=null&&houseno!=0) {
			
			map.put("houseno", houseno);
			int num = houseService.updateHouse(map);//调用方法进行房屋信息修改
			if(num>0) {//说明修改成功
				Map<String,Object> param = new HashMap<String,Object>();
			 	param.put("h_userno", userno);
				List<House> hlist = houseService.selectUserHouse(param);
				
				mov.addObject("hlist", hlist);//当前用户所有房屋信息
				mov.addObject("user", userServcie.selectUser(userno));//模型
				mov.setViewName("guanli");//视图
			}else {
			mov.addObject("mess", "修改失败,请重新修改！");
			mov.setViewName("fabu");
			}
			
		}else {
			
			int num = houseService.insertHouse(map);//调用方法添加房屋信息
			if(num>0) {//说明添加成功
				 Map<String,Object> param = new HashMap<String,Object>();
				 	param.put("h_userno", userno);
				 	
				 	PageHelper.startPage(1, 5, "hDate DESC");//分页
					List<House> hlist = houseService.selectUserHouse(param);
					
					mov.addObject("hlist", hlist);//当前用户所有房屋信息
					mov.addObject("user", userServcie.selectUser(userno));//模型
					mov.setViewName("guanli");//视图
			}else {
				mov.addObject("mess", "添加失败,请重新添加！");
				mov.setViewName("fabu");
			}
			
		}

		
		return mov;
	}
	
	
	
	
	
	
	
	
	
	
	
	//将当前房屋信息进行修改之前的显示
	@RequestMapping("/updateHouseShow")
	public ModelAndView updateHouse(Integer houseno) {
		
		ModelAndView mov = new ModelAndView(); 
		
		House house = houseService.selectOneHouse(houseno);//查找当前的房屋信息
		
		//查找当前用户
		Integer h_userno=house.getH_userno();
		User user = userServcie.selectUser(h_userno);
		
		try {
			if (house != null) {
				List<Type> tist = typeService.selectType();//查找出所有的房屋类型
				 List<County> clist = countyService.selectCounty();//查找出所有的区信息
				 mov.addObject("tlist", tist);
				 mov.addObject("clist",clist);
				 mov.addObject("user", user);
				mov.addObject("house", house);
				mov.setViewName("fabu");
			} 
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mov;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	//显示当前编号的房屋信息
	@RequestMapping("/details")
	public ModelAndView detailsHouse(Integer houseno) {
		
		ModelAndView mov = new ModelAndView(); 
		
		House house = houseService.selectOneHouse(houseno);//查找当前的房屋信息
		
		//查找当前用户
		Integer h_userno=house.getH_userno();
		User user = userServcie.selectUser(h_userno);
		
		try {
			if (house != null) {
				 mov.addObject("user", user);
				mov.addObject("house", house);
				mov.setViewName("details");
			} 
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mov;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	//删除当前编号的房屋信息
	@ResponseBody
	@RequestMapping("/deleteHouse")
	public int deleteHouse(Integer houseno){
		
		int num = houseService.deleteHouse(houseno);//删除当前房屋信息
		
		return num;//因为有jackson所以会自动转换成json类型
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	//查询所有房屋信息之前进行面积和区信息的查询显示
	@RequestMapping("/selectHouse")
	public ModelAndView selectHouse() {
		page.setPageNum(1);//初始化第一页页码进行参数传递
		page.setPageSize(5);//初始化每页条数进行参数传递
		page.setOrderBy("hDate DESC");//初始化排序进行参数传递
		
		
		ModelAndView mov = new ModelAndView(); 
		List<Type> tist = typeService.selectType();//查找出所有的房屋类型
		 List<County> clist = countyService.selectCounty();//查找出所有的区信息
		 
		 
		 Page<House> p = PageHelper.startPage(page.getPageNum(), page.getPageSize(), page.getOrderBy());

		 houseService.selectAllHousePage();//查找出所有分页后的房屋信息
		 List<House> hlist = p.getResult();
		 
		 page.setPages(p.getPages());//从分页对象中获取总页数
		 
		 mov.addObject("hlist", hlist);//房屋信息
		 mov.addObject("tlist", tist);//房屋类型
		 mov.addObject("clist",clist);//区信息
		 mov.addObject("page", page);//将当前页面信息传递过去(其实最主要将第一页页码传递过去)
		 
		 mov.setViewName("list");
		
		return mov;
	}
	
	
	
	
	
	
	
	
	
	
	//按条件查询房屋信息
	@RequestMapping("/findHouse")
	protected ModelAndView findHouse(@RequestParam(name="title")String hTitle,@RequestParam(name="district_id")Integer h_countyno,@RequestParam(name="street_id")Integer h_streetno,@RequestParam(name="type_id")Integer h_typeno,@RequestParam(name="price")String hPrice,@RequestParam(name="floorage")String hArea){
		
			ModelAndView mov = new ModelAndView(); 
			if(!"0".equals(hPrice)) {
				String minPrice =hPrice.split("-")[0];//最小的价格
				String maxPrice =hPrice.split("-")[1];//最大的价格
				param.put("minPrice", minPrice);
				param.put("maxPrice", maxPrice);
			}
			if(!"0".equals(hArea)) {
				String minArea =hArea.split("-")[0];//最小的面积
				String maxArea =hArea.split("-")[1];//最大的面积
				param.put("minArea", minArea);
				param.put("maxArea", maxArea);
			}
			

			param.put("hTitle", hTitle);
			param.put("h_countyno", h_countyno);
			param.put("h_streetno", h_streetno);
			param.put("h_typeno", h_typeno);
			param.put("hPrice",hPrice);
			param.put("hArea",hArea);
			
	

			
			
			 Page<House> p = PageHelper.startPage(page.getPageNum(), page.getPageSize(), page.getOrderBy());
			 houseService.selectPage(param);//按照要求查找到所有的房屋信息
			 List<House> hlist = p.getResult();
			
			
			
			List<County> clist = countyService.selectCounty();
			
			List<Type> tlist = typeService.selectType();
			
			
			
			if(hlist!=null) {//说明查找成功
				List<Street> slist =null;
				if(h_countyno==null||h_countyno==0) {
					
				}else {
					slist = streetService.selectStreet(h_countyno);//根据区编号查找到当前区的所有街道信息
				}
				
				
				
				mov.addObject("list", hlist);
				mov.addObject("hTitle", hTitle);
				mov.addObject("clist", clist);//区集合
				mov.addObject("slist", slist);//街道集合
				mov.addObject("hlist", hlist);//房屋集合
				mov.addObject("h_countyno", h_countyno);//当前区编号
				mov.addObject("h_streetno", h_streetno);//当前街道编号
				mov.addObject("tlist", tlist);//房屋类型
				mov.addObject("h_typeno", h_typeno);//房型编号
				mov.addObject("hPrice", hPrice);//价格
				mov.addObject("hArea", hArea);//面积
				mov.addObject("page", page);//将页面传过去
				
				mov.setViewName("list");
			
		}
					
			return mov;
	}
	
	
	
	
	
	
	
	
	

}
